import routerRoule from '@/mock/router_roule'
import * as ThreeStorage from '@/utils/store/token.js' // cookie local session 统一管理
import Qs from 'qs'

const navs = {
  state: {
    allRouters: [],
    addRouters: ThreeStorage.getRouterList()
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      const val_routers = Qs.stringify(routers)
      ThreeStorage.setRouterList(val_routers)
    },
    SET_ALLROUTERS: (state, routers) => {
      state.allRouters = routers
      console.log('set-allrouter')
    }
  },
  actions: {
    InitRouter({ commit }) {
      return new Promise(resolve => {
        const loginType = ThreeStorage.getLoginType()
        console.log(loginType + '+++++')
        // 有权限访问的路由列表
        let naclist = []
        if (loginType === 'driver') {
          naclist = routerRoule.dirverRoule
        } else if (loginType === 'company') {
          naclist = routerRoule.companyRoule
        }
        commit('SET_ROUTERS', naclist)
        resolve()
      }).catch(error => {
        console.log(error)
      })
    }
  }
}

export default navs
